<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="寄售用户ID" prop="consignUserId" label-width="68px">
        <el-input v-model="queryParams.consignUserId" placeholder="UID" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="订单状态" clearable style="width: 140px">
          <el-option v-for="dict in consignStateList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="购买时间">
        <el-date-picker v-model="buyDateRange" style="width: 240px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
   
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" v-loading="loading" @sort-change="changeTableSort" @row-click="rowClick" :data="orderList" @row-dblclick="handleUpdate" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="寄售用户" prop="userId" align="center" width="160" :sortable="'custom'" />
      <el-table-column label="寄售时间" prop="createTime" align="center" width="160" :sortable="'custom'" />
      <!--<el-table-column label="场馆" prop="venueName" align="center" width="120" :sortable="'custom'" />-->
      <el-table-column label="商品编号" prop="goodsNo" align="center" width="120" :sortable="'custom'" />
      <el-table-column label="商品名称" prop="goodsName" align="center" width="180" :sortable="'custom'" />
      <!--<el-table-column label="购买数量" prop="amount" align="center" />-->
      <el-table-column label="订单状态" prop="state" align="center" width="120" :sortable="'custom'">
        <template slot-scope="scope">
          {{ getLabelByValue(consignStateList,scope.row.state) }}
        </template>
      </el-table-column>
      <el-table-column label="置换用户" prop="buyUserId" align="center"  width="150" />
      <el-table-column label="置换时间" prop="buyTime" align="center" width="200" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { listOrderConsign} from "@/api/order/order.js";
import { mapState } from 'vuex'
import { pickGoodesEnum } from '@/common/enums'
import Logistics from "@/components/Logistics";
import { checkPermi } from "@/utils/permission"; // 权限判断函数

export default {
  name: "OrderReplace", //消费品订单
  dicts: ['order_status'],
  components: {  Logistics },
  data() {
    return {
      pickGoodesEnum,
      // 列表数据
      orderList: [],
      dialogVisible: false,
      logisticsShow: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        preId: undefined,
      },
      pageNum: 1,
      // 表单参数
      form: {},

      // 购买日期范围
      buyDateRange: [],

      goodsNameList: [],
      consignStateList:[
        { label: '寄售中', value: 1 },
        { label: '寄售成功', value: 2 },
      ],
      chargeTypeList:[
        { label: '现金', value: 1 },
        { label: '金积分', value: 2 },
      ],
      diffPayUnitList:[
        { label: '现金', value: 0 },
        { label: '金积分', value: 1 },
        { label: '银积分', value: 2 },
      ]
    };
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 330px)'
    },
    ...mapState({
      venueList: state => state.venue.list,
      logistcsType: state => state.logistics.list

    }),
    orderStatusList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.dict.type.order_status));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    //提货状态
    pickGoodesList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.pickGoodesEnum));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    buyDateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.consignStartTime = String(newV[0]);
          this.queryParams.consignEndTime = String(newV[1]);
        }
      } else {
        this.queryParams.consignStartTime = undefined;
        this.queryParams.consignEndTime = undefined;
      }
      this.getList();

    },
  },

  created() {
    this.getList();
    this.$store.dispatch('GetVenueList');
    this.$store.dispatch('GetListLogisticsType')
  },

  methods: {
    checkPermi,
    getList() {
      this.loading = true;
      listOrderConsign(this.queryParams).then((response) => {
        this.orderList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },

    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.buyStartTime = undefined;
      this.queryParams.buyEndTime = undefined;
      this.buyDateRange = undefined;
      this.pickDateRange = undefined;
      this.handleQuery();
    },

    getLabelByValue(arr, value) {
      // 字段不存在返回‘’
      if (value === undefined || value === null) {
        return ''
      }
      for (let i in arr) {
        let e = arr[i]
        if (e && e.value === value) {
          return e.label
        }
      }

      return ''
    }
  },
};
</script>
<style scoped>
.pointer {
  cursor: pointer;
  color: cornflowerblue;
}
</style>
